package com.goddess.yeecolor.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.goddess.yeecolor.Modle.Comment;
import com.goddess.yeecolor.Modle.Reply;
import com.goddess.yeecolor.R;
import com.goddess.yeecolor.adapter.Item_Exerciseitem_commitAdapter;
import com.goddess.yeecolor.adapter.item_Exerciseitem_commit_contentAdapter;
import com.goddess.yeecolor.base.BaseActivity;
import com.goddess.yeecolor.tools.JumpIntents;

import java.util.ArrayList;
import java.util.List;

/**
 *单行评论详情
 */
public class CommitItemDetailsActivity extends BaseActivity{

    private View mView;
    private TextView mTitle;
    private Toolbar mToobar;
    private Bundle mBundle;
    private Comment mComment;
    private RecyclerView commititemdetails_recyclerview;
    private LinearLayoutManager mLinearManager;
    private List<Comment> mList;
    private Item_Exerciseitem_commitAdapter adapter;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        setTheme(R.style.AppTheme);
        super.onCreate(savedInstanceState);
        mView= LayoutInflater.from(this).inflate(R.layout.activity_commititemdetails,null);
        setContentView(mView);
        initFindView();
        initData();
    }

    @Override
    public void initOnClck() {

    }

    @Override
    public void initFindView() {
        mToobar= (Toolbar) findViewById(R.id.title_toobar);
        mTitle= (TextView) findViewById(R.id.title_content);
        mTitle.setText("评论详情");
//        setSupportActionBar(mToobar);
//        getSupportActionBar().setDisplayShowTitleEnabled(false);
        mToobar.setNavigationIcon(R.mipmap.back3x);
        mToobar.setNavigationOnClickListener((view)->{
            setFiish(this);
        });
        commititemdetails_recyclerview= (RecyclerView) findViewById(R.id.commititemdetails_recyclerview);
        mLinearManager = new LinearLayoutManager(this);
        commititemdetails_recyclerview.setLayoutManager(mLinearManager);
    }

    @Override
    public void initData() {
        mBundle=getIntent().getBundleExtra("bundle");
        mComment=mBundle.getParcelable("itemComment");
        mList=new ArrayList<>();
        mList.add(mComment);
        commentReplyAdapter = null;//默认
        adapter = new Item_Exerciseitem_commitAdapter(this,this,mList,replyToCommentListener, commentReplyAdapter,
                replyToReplyListener,true);
        commititemdetails_recyclerview.setAdapter(adapter);
    }


    /**
     * 发表评论的监听
     */
    private int TAG_SEND=4;
    private int TAG_FLOOR=5;



    /**
     * 回复评论的监听（回复楼主）
     */
    private View.OnClickListener replyToCommentListener = (v)->{

        int position = (Integer) v.getTag();

        Bundle bundle=new Bundle();
        bundle.putInt("position",position);
        JumpIntents.startIntentResultIntent(this,CommitActivity.class,TAG_SEND,bundle);

    };

    /**
     * 互相回复的监听（楼中楼）
     */
    private View.OnClickListener replyToReplyListener = (v)->{


        int parentPosition = (Integer) v.getTag(R.id.tag_first);
        int childPosition = (Integer) v.getTag(R.id.tag_second);
//        int position = (Integer) v.getTag();

        Bundle bundle=new Bundle();
        bundle.putInt("parentPosition",parentPosition);
        bundle.putInt("childPosition",childPosition);
        JumpIntents.startIntentResultIntent(this,CommitActivity.class,TAG_FLOOR,bundle);
    };


    /**
     * 结果回调
     * @param requestCode
     * @param resultCode
     * @param data
     */
    private String content;
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){

            case 4:
                if (resultCode==TAG_SEND){//回复楼主
                    Log.i("zy","ssssss");
                    Bundle bundle=data.getBundleExtra("bundle");
                    int position=bundle.getInt("position",-1);
                    content=bundle.getString("content");
                    Log.i("zy",position+content);
                    sendCommit(ONE_COMMENT_CODE,position,content);
                }
                break;
            case 5:
                if (resultCode==TAG_FLOOR){
                    Bundle bundle=data.getBundleExtra("bundle");
                    int position=bundle.getInt("parentPosition",-1);
                    int childpositon=bundle.getInt("childPosition",-1);
                    content=bundle.getString("content");
                    sendCommit(position, childpositon,content);
                }
                break;
        }
    }

    /**
     * 评论
     * @param parentPositon
     * @param childPostion
     */
    private static final int ONE_COMMENT_CODE = -1;
    private List<Reply> replyList;
    private item_Exerciseitem_commit_contentAdapter commentReplyAdapter;
    private void sendCommit(final int parentPositon,
                            final int childPostion,String content) {
        Reply reply = new Reply();
        reply.setUsername("兽" + parentPositon + childPostion);
        reply.setContent(content);
        if (parentPositon != -1) {//回复楼上

            reply.setReplyTo(mList.get(parentPositon)
                    .getReplyList().get(childPostion)
                    .getUsername());
            mList.get(parentPositon).getReplyList()
                    .add(reply);
        } else {
            replyList = mList.get(childPostion)
                    .getReplyList();
            replyList.add(reply);
            mList.get(childPostion).setReplyList(
                    replyList);
        }
        adapter.clearList();
        adapter.updateList(mList);
        adapter.notifyDataSetChanged();

    }
}
